var indexeddb;
var initIndexedDB = function(name,callback){
	var request=window.indexedDB.open(name,1);
    request.onerror=function(e){
        alert("Database error");
    };
    request.onsuccess=function(e){
    	indexeddb = e.target.result;
		insertDataIndexedDB();
    	callback();
    };
    request.onupgradeneeded = function(e) {
    	var d = e.target.result;
    	var workflow_plugin = d.createObjectStore("plugin",{keyPath:"id"});
    	workflow_plugin.createIndex("business_type","business_type",{unique:false});
    	workflow_plugin.createIndex("node_type","node_type",{unique:false});
    	workflow_plugin.createIndex("icon_src","icon_src",{unique:false});
    	workflow_plugin.createIndex("plugin_name","plugin_name",{unique:false});
    	workflow_plugin.createIndex("plugin_type","plugin_type",{unique:false});
    	
    	var workflow = d.createObjectStore("workflow",{keyPath:"id"});
    	workflow.createIndex("header","header",{unique:false});
    	workflow.createIndex("isopen","isopen",{unique:false});
    	workflow.createIndex("pluginids","pluginids",{unique:false});
    	
    	var workflow_job = d.createObjectStore("job",{keyPath:"id",autoIncrement:true});
    	workflow_job.createIndex("p_id","p_id",{unique:false});
    	
    	var workflow_log = d.createObjectStore("log",{keyPath:"id",autoIncrement:true});
    	workflow_log.createIndex("plugin_name","plugin_name",{unique:false});
    	
    	var workflow_prog = d.createObjectStore("prog",{keyPath:"id",autoIncrement:true});
    	workflow_prog.createIndex("plugin_name","plugin_name",{unique:false});
    };
}

var insertDataIndexedDB = function(){
	var pluginData = [
		{"id":1,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/git","plugin_name":"git","plugin_type":1},
		{"id":2,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/svn","plugin_name":"svn","plugin_type":1},
		{"id":15,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/start","plugin_name":"start","plugin_type":1},
		{"id":16,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/end","plugin_name":"endNode","plugin_type":1},
		{"id":3,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/C++","plugin_name":"C++","plugin_type":1},
		{"id":4,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/golang","plugin_name":"golang","plugin_type":1},
		{"id":5,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/java","plugin_name":"java","plugin_type":1},
		{"id":6,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/python","plugin_name":"python","plugin_type":1},
		{"id":7,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/harbor","plugin_name":"harbor","plugin_type":1},
		{"id":8,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/registry","plugin_name":"registry","plugin_type":1},
		{"id":13,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/test","plugin_name":"test","plugin_type":1},
		{"id":14,"business_type":1,"node_type":1,"icon_src":"../../images/flowconfig/bug","plugin_name":"bug","plugin_type":1}
	];
	
	var workflowData = [
		{"id":1,"header":"控制流","isopen":true,"pluginids":"15,16"},
		{"id":2,"header":"代码仓库","isopen":true,"pluginids":"1,2"},
		{"id":3,"header":"开发语言","isopen":true,"pluginids":"3,4,5,6"},
		{"id":4,"header":"质量检测","isopen":true,"pluginids":"13,14"},
		{"id":5,"header":"镜像管理","isopen":true,"pluginids":"7,8"}
	];
	
	var logData = [
		{"id":1,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"git","icon_src":"../../images/flowconfig/git",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":2,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"svn","icon_src":"../../images/flowconfig/svn",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":3,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"C++","icon_src":"../../images/flowconfig/C++",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":4,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"golang","icon_src":"../../images/flowconfig/golang",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":5,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"java","icon_src":"../../images/flowconfig/java",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":6,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"python","icon_src":"../../images/flowconfig/python",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":7,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"test","icon_src":"../../images/flowconfig/test",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":8,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"bug","icon_src":"../../images/flowconfig/bug",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":9,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"harbor","icon_src":"../../images/flowconfig/harbor",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"},
		{"id":10,"business_type":1,"status":1,"start_time":"2017-05-12 12:20:20","datetime_now":"2017-05-12 12:30:21","finish_time":"2017-05-12 12:31:21","plugin_name":"registry","icon_src":"../../images/flowconfig/registry",
			"logs":"2017-05-12 12:23:33,843 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: qualityDataUsingGET_1" +
                "2017-05-12 12:23:33,847 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: getPeopleForProductUsingGET_1\n" +
                "2017-05-12 12:23:33,875 INFO  s.d.s.w.r.o.CachingOperationNameGenerator - Generating unique operation named: searchUsingGET_1\n" +
                "2017-05-12 12:23:33,924 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,936 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [\"http-nio-8080\"]\n" +
                "2017-05-12 12:23:33,954 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read"}
	];
	
	var progData = [
		{}
	];
	var transaction = indexeddb.transaction(["plugin","workflow","log"], "readwrite");
	var pluginStore = transaction.objectStore('plugin');
	
	var i = 0;
	for(i = 0;i<pluginData.length;i++){
		var request = pluginStore.add(pluginData[i]);
		request.onsuccess=function(event){
		}
	}
	
	var workflowStore = transaction.objectStore('workflow');
	for(i = 0;i<workflowData.length;i++){
		var request = workflowStore.add(workflowData[i]);
		request.onsuccess=function(event){
		}
	}
	
	var logStore = transaction.objectStore('log');
	for(i = 0;i<logData.length;i++){
		var request = logStore.add(logData[i]);
		request.onsuccess=function(event){
		}
	}
}
function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
  });
}

function getNowDate(){
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if(strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	return date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
}
